package com.ppandroid.wifiap.manager

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.permissionx.guolindev.PermissionX

/**
 * 权限管理器
 */
class PermissionManager(private val context: FragmentActivity) {
    
    /**
     * 检查是否有所需权限
     */
    fun hasRequiredPermissions(): Boolean {
        return getRequiredPermissions().all { permission ->
            ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
        }
    }
    
    /**
     * 获取所需的权限列表
     */
    fun getRequiredPermissions(): List<String> {
        val permissions = mutableListOf(
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.CHANGE_NETWORK_STATE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.INTERNET
        )
        
        // Android 6.0+ 需要位置权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permissions.add(Manifest.permission.ACCESS_FINE_LOCATION)
            permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)
        }
        
        // Android 13+ 需要附近WiFi设备权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions.add(Manifest.permission.NEARBY_WIFI_DEVICES)
        }
        
        return permissions
    }
    
    /**
     * 请求权限
     */
    fun requestPermissions(
        onResult: (allGranted: Boolean, grantedList: List<String>, deniedList: List<String>) -> Unit
    ) {
        val permissions = getRequiredPermissions().toTypedArray()
        
        PermissionX.init(context)
            .permissions(*permissions)
            .request { allGranted, grantedList, deniedList ->
                onResult(allGranted, grantedList, deniedList)
            }
    }
    
    /**
     * 检查特定权限
     */
    fun hasPermission(permission: String): Boolean {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
    }
    
    /**
     * 检查WiFi相关权限
     */
    fun hasWifiPermissions(): Boolean {
        return hasPermission(Manifest.permission.ACCESS_WIFI_STATE) &&
                hasPermission(Manifest.permission.CHANGE_WIFI_STATE) &&
                hasPermission(Manifest.permission.CHANGE_NETWORK_STATE) &&
                hasPermission(Manifest.permission.ACCESS_NETWORK_STATE)
    }
    
    /**
     * 检查位置权限
     */
    fun hasLocationPermissions(): Boolean {
        return hasPermission(Manifest.permission.ACCESS_FINE_LOCATION) ||
                hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
    }
    
    /**
     * 检查附近WiFi设备权限
     */
    fun hasNearbyWifiDevicesPermission(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            hasPermission(Manifest.permission.NEARBY_WIFI_DEVICES)
        } else {
            true
        }
    }
}
